package servlet;

import util.ParameterUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @author 石小俊
 * @date 2023年10月20日 11:35
 */
@WebServlet("/findById")
public class ShowModifyEmpServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String url = "jdbc:mysql://127.0.0.1:3306/web?useUnicode=true&characterEncoding=utf-8";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, "root", "");
            String sql = new StringBuffer()
                    .append(" select id,name,salary,gender,edu,hobbies ")
                    .append(" from t_emp ")
                    .append(" where id = ? ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setInt(1, Integer.parseInt(id));
            rs = ps.executeQuery();
            if (rs.next()) {
                out.println(" <!DOCTYPE html> ");
                out.println(" <html lang=\"en\"> ");
                out.println(" <head> ");
                out.println("     <meta charset=\"UTF-8\"> ");
                out.println("     <title>修改员工信息</title> ");
                out.println(" </head> ");
                out.println(" <body> ");
                out.println(" <h1>修改员工</h1> ");
                out.println(" <a href=\"findAll\">返回员工列表</a> ");
                out.println(" <hr> ");
                out.println(" <form action=\"modifyEmp\" method=\"post\"> ");
                out.println("     编号:<input type=\"text\" name=\"id\" value=\"" + rs.getInt("id") + "\" readonly><br> ");
                out.println("     姓名:<input type=\"text\" name=\"name\" value=\"" + rs.getString("name") + "\"><br> ");
                out.println("     工资:<input type=\"text\" name=\"salary\" value=\"" + rs.getDouble("salary") + "\"><br> ");
                out.println("     性别: ");
                out.println("     <input type=\"radio\" name=\"gender\" value=\"man\" " + ("man".equals(rs.getString("gender")) ? "checked" : "") + ">男 ");
                out.println("     <input type=\"radio\" name=\"gender\" value=\"woman\" " + ("woman".equals(rs.getString("gender")) ? "checked" : "") + ">女<br> ");
                out.println("     学历: ");
                out.println("     <select name=\"edu\"> ");
                out.println("         <option value=\"-1\">请选择</option> ");
                out.println("         <option value=\"1\" " + ("1".equals(rs.getString("edu")) ? "selected" : "") + ">大专</option> ");
                out.println("         <option value=\"2\" " + ("2".equals(rs.getString("edu")) ? "selected" : "") + ">本科</option> ");
                out.println("         <option value=\"3\" " + ("3".equals(rs.getString("edu")) ? "selected" : "") + ">研究生</option> ");
                out.println("     </select><br> ");
                out.println("     兴趣: ");
                out.println("     <input type=\"checkbox\" name=\"hobbies\" value=\"eat\" " + ParameterUtil.checkHobby(rs.getString("hobbies"), "eat") + ">吃饭 ");
                out.println("     <input type=\"checkbox\" name=\"hobbies\" value=\"sleep\" " + ParameterUtil.checkHobby(rs.getString("hobbies"), "sleep") + ">睡觉 ");
                out.println("     <input type=\"checkbox\" name=\"hobbies\" value=\"study\" " + ParameterUtil.checkHobby(rs.getString("hobbies"), "study") + ">学习<br> ");
                out.println("     <input type=\"submit\" value=\"修改\"> ");
                out.println(" </form> ");
                out.println(" </body> ");
                out.println(" </html> ");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
